﻿yearly_global_pulse = {
	first_valid_on_action = { 
		calyver_rabbinism_lose_polygamy_check
	}
}

random_yearly_playable_pulse = {

	random_events = {	# A single event will be picked to fire
		
		chance_to_happen = 70	# A percentage chance determining whether the events involved will be evaluated at all

		chance_of_no_event = {
			value = 0
			if = {
				limit = {
					is_ai = yes
					highest_held_title_tier < tier_duchy
				}
				add = 75 # AI counts aren't particularly important
			}
			if = { #Less drama for only polygamous rulers
				limit = {
					hp_accepts_polygamy = yes
					hp_accepts_concubinage = no
				}
				add = 25
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					OR = {
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_tribal
					}
				}
				add = 50
			}
			if = {
				limit = {
					hp_accepts_harems = no
				}
				add = 100 # Monogamous rulers don't apply
			}
		}

		100 = harem_politics.0001 	# Begging event
		75 = harem_politics.0002	# Hui Guifei tortures Hailan
		100 = harem_politics.0004   # Promoting a concubine
		100 = harem_politics.0005   # Demoting a spouse
		75 = harem_politics.0006	# Choosing an adoptive parent for a parentless child
		100 = harem_politics.0008	# A consort or parent asks you to title a relative
		25 = harem_politics.0009	# Meeting Consort Ling - Chun Fei version
		25 = harem_politics.0010	# Meeting Consort Ling - Jia Fei version
		100 = harem_politics.0011	# Choosing a night companion
		100 = harem_politics.0012	# Poisoning an unwanted consort
		75 = harem_politics.1013	# Qi is jealous of Xin
		25 = harem_politics.0016	# Your concubines are performing
		25 = harem_politics.0017	# Meeting Consort Ling - She's a maid
		100 = harem_politics.0019	# Yu disrespects Zhen Huan
		75 = harem_politics.0020	# You meet your consorts in a walk
		100 = harem_politics.0022	# Noble Consort Hua makes Zhen Huan miscarry
		100 = harem_politics.0024	# Summoning a consort to perform for you
		150 = harem_politics.0028	# A consort helps you relieve stress
		75 = harem_politics.0029	# A ruler asks you to promote their relative
		#75 = harem_politics.0030	# Choosing between a vassal's child or their servant
		75 = harem_politics.0031	# A consort asks you to promote a relative
		75 = harem_politics.0032	# Meeting Concubine Mei
		#100 = harem_politics.0034	# Someone asks you to release a consort
		#75 = harem_politics.0035	# A consort is stirring the pot
		100 = harem_politics.0036	# The Empress Dowager lectures the Empress
		120 = harem_politics.0037	# Muslim flavor - A matron presents you with concubines
		100 = harem_politics.1039	# A reserved night
		100 = harem_politics.0040	# The Empress Dowager pushes you to favor an ally
		150 = harem_politics.0041	# Chaotic harem
		75 = harem_politics.1042	# Meeting Hailan
		795 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}

quarterly_playable_pulse = {
	first_valid_on_action = { 
		consort_events_on_actions
	}
}

# Called from code once a year for all characters, at a random point that year (individually for each character)
# Root is the character
random_yearly_everyone_pulse = {
	first_valid_on_action = { 
		consort_events_on_actions
	}
}

# Harem Politics on actions
consort_events_on_actions = {
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 65	# A percentage chance determining whether the events involved will be evaluated at all

		chance_of_no_event = {
			value = 0
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
					NOT = {
						any_consort = {
							is_ai = no
						}
					}
				}
				add = 60
			}
			if = { #Less drama for only polygamous rulers
				limit = {
					NOT = {
						any_consort = {
							is_landed_or_landless_administrative = yes
							hp_accepts_concubinage = yes
						}
					}
				}
				add = 25
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					NOT = {
						any_consort = {
							is_landed_or_landless_administrative = yes
							NOR = {
								government_has_flag = government_is_nomadic
								government_has_flag = government_is_tribal
							}
						}
					}
				}
				add = 50
			}
			if = {
				limit = {
					NOT = {
						any_consort = {
							is_landed_or_landless_administrative = yes
							hp_accepts_harems = yes
						}
					}
				}
				add = 100 # Monogamous rulers don't apply
			}
		}

		100 = consort_events.0001 	# Hui Guifei punishes Mei
		100 = consort_events.1002   # Error suppression - Hua Fei tortures Xia
		100 = consort_events.0004   # A badmouthing concubine
		100 = consort_events.0005	# The morning greeting
		100 = consort_events.0006	# Yanwan is courting favor
		110 = consort_events.0007	# Hidden events - An authority punishes a random consort
		110 = consort_events.0008	# Hidden events - Two consorts become friends
		110 = consort_events.0009	# Hidden events - Two consorts become enemies
		100 = consort_events.0010	# Hidden events - Unfavored consorts
		100 = consort_events.0011   # Scheming and diplomatic consorts
		100 = consort_events.0012	# Choosing an ally
		80 = consort_events.0013	# Killing the main heir
		100 = consort_events.0014	# I'm favored!
		100 = consort_events.0015	# I'm unfavored
		100 = consort_events.0016	# No longer demoted
		40 = consort_events.0017	# Killing a higher-ranked consort
		40 = consort_events.0019	# Poisoning a consort
		120 = consort_events.0027	# A consort causes a miscarriage
		5 = consort_events.0028		# Hidden events - A consort becomes a poet
		15 = consort_events.0029	# Hidden events - A consort becomes a seducer
		25 = consort_events.0030	# Hidden events - Consort learns of a courtier's secrets
		25 = consort_events.0031	# Hidden events - Consort learns of one of your known secrets
		25 = consort_events.0032	# Hidden events - Consort learns of one of your secrets
		50 = consort_events.0033	# Hidden events - Consort leaks a secret to a vassal
		70 = consort_events.0035	# Hidden events - Influential consort gives influence to family
		75 = consort_events.0036	# Hidden events - A consort murders an heir
		100 = consort_favor_valuation.0006	# Hidden events - Increasing a consort's favor
		660 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}

#The standard in game check can't be met if this mod is working properly (no faith will have doctrine_polygamy) so we can just add our own redirect to the stack
calyver_rabbinism_lose_polygamy_check = {
	effect = {
		# Rabbinicism becomes Monogamous
		if = {
			limit = {
				# Rabbi Gershom bans polygamy sometime in the 11th century
				current_date > "1000.01.01"
				# Make sure this hasn't happened already
				faith:rabbinism = {
					has_doctrine_parameter = allows_polygamy
					NOR = {
						has_variable = rabbinicism_accepted_monogamy
						has_variable = rabbinicism_rejected_monogamy
					}
				}
			}
			religion:judaism_religion = {
				every_faith = {
					limit = {
						has_doctrine_parameter = allows_polygamy
						has_doctrine = temple_authority_rabbinic_doctrine
					}

					if = {
						limit = {
							has_doctrine = doctrine_polygamy
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_4
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_4
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_10
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_10
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_15
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_15
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_25
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_25
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_50
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_50
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_100
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_100
						}
					}

					if = {
						limit = {
							has_doctrine = doctrine_marriage_realm_celebration_disallowed
							NOT = { has_doctrine_parameter = allows_concubinage }
						}
						set_variable = {
							name = faith_swap_realm_celebration
							value = yes
						}
					} 
					else = {
						set_variable = {
							name = faith_swap_realm_celebration
							value = no
						}
					}

					if = {
						limit = {
							exists = religious_head
						}
						if = {
							limit = {
								religious_head = { is_independent_ruler = no }
								religious_head.top_liege.faith = this
							}
							religious_head.top_liege = { trigger_event = jewish_events.0001 }
						}
						else_if = {
							limit = {
								religious_head = { is_independent_ruler = no }
								religious_head.liege.faith = this
							}
							religious_head.liege = { trigger_event = jewish_events.0001 }
						}
						else = {
							religious_head = { trigger_event = jewish_events.0001 }
						}
					}
					else_if = {
						limit = {
							any_player = { # Checking players is more performant than checking faith characters and limiting it to players
								faith = prev
							}
						}
						random_player = {
							limit = { faith = prev }
							trigger_event = jewish_events.0001
						}
					}
					else = {
						random_faith_character = {
							trigger_event = jewish_events.0001
						}
					}
				}
			}
		}
	}
}